package com.dudu.controller;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.dudu.bean.RemindInfo;
import com.dudu.bean.UserBean;
import com.dudu.enumeration.OperateTypeEnum;
import com.dudu.service.ILogService;
import com.dudu.service.IRemindService;
import com.dudu.service.impl.LogService;
import com.dudu.service.impl.RemindServiceImpl;

import core.CoreService;

/**
 * 提醒服务
 * 
 * @author gaoxs
 * 
 */
public class RemindController extends CoreService implements IRemindController {
    // 当前登陆用户
    private final String KEY_CURRENT_USER = "key_current_user";
    // 用于记录相应接口调用的日志
    private ILogService logService = new LogService();
    private IRemindService remindService=new RemindServiceImpl();
    @Override
    public String queryAll() {
	// 最后返回的对象
	JSONObject object = new JSONObject();
	// 获取当前登录用户信息
	UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
	// 判断如果用户信息不为空则继续下一步
	if (currentUser != null) {
	    JSONArray jsonArray = new JSONArray();
	    // 获取分页信息
	    String pageSql = this.getPageSql();
	    // 获取查询条件
	    String paramSql = this.convertParams();
	    // 总数量
	    int totalAcount = remindService.getTotalAcount(paramSql);
	    // 查询日志
	    List<RemindInfo> logs = remindService.queryAll(paramSql, pageSql);
	    // 转成JSON
	    jsonArray = JSONArray.fromObject(logs);
	    JSONObject dataobject = new JSONObject();
	    object.put("flag", "000000");
	    object.put("desc", "success");
	    dataobject.put("totalItemNum", totalAcount);
	    dataobject.put("items", jsonArray);
	    object.put("data", dataobject);
	} else {
	    object.put("flag", "100000");
	}

	return returnData(object.toString());
    }

    @Override
    public String updateState() {
	// 最后返回的对象
	JSONObject object = new JSONObject();
	// 获取当前登录用户信息
	UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
	// 判断如果用户信息不为空则继续下一步
	if (currentUser != null) {
	    // 获取参数用户ID
	    String code = this.getRequest().getParameter("code");
	    if (StringUtils.isNotBlank(code)) {
		remindService.updateRemind(code);
		logService.log(OperateTypeEnum.EDIT.getKey(), currentUser.getId(), currentUser.getName(), "sensitive_code_info", "状态：1" + "|主键：" + code);
	    }
	    object.put("flag", "000000");
	    object.put("desc", "success");
	} else {
	    object.put("flag", "100000");
	}

	return returnData(object.toString());
    }

}
